﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.CommonImp;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class OutlayControl : UserControl
    {
        private readonly IOutlayService<Outlay> _outlay;

        public OutlayControl(IOutlayService<Outlay> outlay)
        {
            _outlay = outlay;
            InitializeComponent();
        }

        private void OutlayControl_Load(object sender, EventArgs e)
        {
            var income = new OutlayService();
            comboBox1.DataSource = null;
            List<OutLayType> getincome = income.GetName();
            comboBox1.DataSource = getincome;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Id";
            GetAll();
        }

        private void btnOutlayInsert_Click(object sender, EventArgs e)
        {
            var form = new OutlayAddForm(new OutlayService())
            {
                RefeshMainForm = GetAll
            };
            form.ShowDialog(this);
        }

        private void btnOutlayUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show(@"未选择");
                return;
            }
            var getcell = dataGridView1.CurrentRow.Index;
            var outlay = new Outlay
            {
                Id = long.Parse(dataGridView1.Rows[getcell].Cells["Id"].Value.ToString()),
                Money = decimal.Parse(dataGridView1.Rows[getcell].Cells["Money"].Value.ToString()),
                Comment = dataGridView1.Rows[getcell].Cells["Comment"].Value.ToString(),
                OutLayTypeId = Convert.ToInt32(dataGridView1.Rows[getcell].Cells["OutLayTypeId"].Value.ToString()),
                AccountTypeId = Convert.ToInt32(dataGridView1.Rows[getcell].Cells["AccountTypeId"].Value.ToString()),
            };



            var form = new OutlayUpdateForm(new OutlayService())
            {
                Outlay = outlay,
                RefeshMainForm = GetAll
            };
            form.ShowDialog(this);
        }

        public void GetAll()
        {
            dataGridView1.DataSource = null;
            var re = new DataGridViewService();
            dataGridView1.Columns.AddRange(re.BindColumns(typeof (Outlay)));
            List<Outlay> getall = _outlay.GetAll();
            dataGridView1.DataSource = getall.ToArray();
        }

        private void OutlayId_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtOutlayid.Text))
            {
                MessageBox.Show(@"请输入账户 --");
                return;
            }
            long id = long.Parse(txtOutlayid.Text);

            if (_outlay.Get(id) != null)
            {
                var get = new List<Outlay> {_outlay.Get(id)};
                dataGridView1.DataSource = get.ToArray();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show(@"未选择");
                return;
            }
            var getcell = dataGridView1.CurrentRow.Index;
            _outlay.Delete(Convert.ToInt32(dataGridView1.Rows[getcell].Cells[0].Value.ToString()));
            GetAll();
        }

        private void btnOutlay_Click(object sender, EventArgs e)
        {
            long outlaysource = long.Parse(comboBox1.SelectedValue.ToString());
            var ountlay = new OutlayService();
            if (ountlay.GetDim(outlaysource) != null)
            {
                List<Outlay> getdim = ountlay.GetDim(outlaysource);
                dataGridView1.DataSource = getdim;
            }
        }

        private void btnOutlayMoney_Click(object sender, EventArgs e)
        {
            decimal outlaymoney = decimal.Parse(txtMoney.Text);
            var ountlay = new OutlayService();
            if (ountlay.GetDim(outlaymoney) != null)
            {
                List<Outlay> getdim = ountlay.GetDim(outlaymoney);
                dataGridView1.DataSource = getdim;
            }
        }
    }
}